package com.qy139.picture.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qy139.entity.Picture;
import com.qy139.picture.mapper.PictureMapper;
import com.qy139.picture.service.IPictureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qy139.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuhaochen
 * @since 2021-11-30
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements IPictureService {
   /* final static String accessKeyId = "LTAI5tQzsjw6HaY1dzTmmjss";
    final static String accessKeySecret = "jAXhNqCr7p9FWKgYscBTbgwW515kdi";
    final static String bucketName = "mypackage0927";
    final static  String  endpoint = "oss-cn-beijing.aliyuncs.com";*/
   String endpoint = "oss-cn-beijing.aliyuncs.com";
    String accessKeyId = "LTAI5tJvkQMBfdpsuVuQwWcL";
    String accessKeySecret = "JXrMa0B5r7EO7V8fHpLbeDqpuhIZ2q";
    String buckeName="ourpackage";
    @Resource
    PictureMapper pictureMapper;
    @Override
    public List<Map> uploadFile(MultipartFile[] file, Picture picture) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        List<Map> list = new ArrayList<>();
        System.out.println(file.length);
        if (file.length>0&&file!=null){
            for (MultipartFile file1 : file) {
                String originalFilename = file1.getOriginalFilename(); // 1.jpg 1.0.jpg
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
                String newname = System.currentTimeMillis()+extension;
                PutObjectRequest putObjectRequest = new PutObjectRequest(buckeName, newname,file1.getInputStream() );
                // 上传文件。
                ossClient.putObject(putObjectRequest);
                String url="https://"+buckeName+"."+endpoint+"/"+newname;

                Map map = new HashMap();
                map.put("url",url);
                //map.put("filename",newname);
                System.out.println("新增的图片的信息"+map);
                Picture picture1 = new Picture();
                picture1.setUrl(url);
                this.save(picture1);
                System.out.println("新增的图片的id："+picture1.getId());
                map.put("imgid",picture1.getId());
                list.add(map);
            }
        }
        // 关闭OSSClient。
        ossClient.shutdown();
        return list;
    }

    @Override
    public List<Picture> listByImgs(String imgid) {
        System.out.println("图片的id"+imgid);
        String[] ids = imgid.split(",");// id的数组
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<Picture>();
        for (int i = 0; i <ids.length ; i++) {// 2
            // i=0
            if(i==ids.length-1) {
                queryWrapper.eq("id", ids[i]);
            }else{
                queryWrapper.eq("id", ids[i]).or();
            }
        }
        return this.list(queryWrapper);
    }

    @Override
    public Result updateStu(Picture picture) {
        int i=pictureMapper.updateStu(picture);
        if (i>0){
            return new Result(0,"修改成功");
        }else {
            return new Result(200,"修改失败");
        }
    }
}
